<%
Class FastAsp_FSO
    Private m_FSO        'FSO对象
    Private m_Root       '根目录
    
    Private Sub Class_Initialize()
        Set m_FSO = Server.CreateObject("Scripting.FileSystemObject")
        m_Root = Server.MapPath("/")
    End Sub
    
    '创建文件夹
    Public Function CreateFolder(byval path)
        Dim fullPath : fullPath = GetFullPath(path)
        If Not m_FSO.FolderExists(fullPath) Then
            CreateParentFolder fullPath
            m_FSO.CreateFolder fullPath
        End If
        Set CreateFolder = Me
    End Function
    
    '删除文件夹
    Public Function DeleteFolder(byval path)
        Dim fullPath : fullPath = GetFullPath(path)
        If m_FSO.FolderExists(fullPath) Then
            m_FSO.DeleteFolder fullPath
        End If
        Set DeleteFolder = Me
    End Function
    
    '复制文件夹
    Public Function CopyFolder(byval source, byval target)
        Dim sourcePath : sourcePath = GetFullPath(source)
        Dim targetPath : targetPath = GetFullPath(target)
        If m_FSO.FolderExists(sourcePath) Then
            CreateParentFolder targetPath
            m_FSO.CopyFolder sourcePath, targetPath
        End If
        Set CopyFolder = Me
    End Function
    
    '移动文件夹
    Public Function MoveFolder(byval source, byval target)
        Dim sourcePath : sourcePath = GetFullPath(source)
        Dim targetPath : targetPath = GetFullPath(target)
        If m_FSO.FolderExists(sourcePath) Then
            CreateParentFolder targetPath
            m_FSO.MoveFolder sourcePath, targetPath
        End If
        Set MoveFolder = Me
    End Function
    
    '创建文件
    Public Function CreateFile(byval path, byval content)
        Dim fullPath : fullPath = GetFullPath(path)
        CreateParentFolder m_FSO.GetParentFolderName(fullPath)
        
        Dim stream : Set stream = CreateObject("ADODB.Stream")
        stream.Type = 2 'adTypeText
        stream.Charset = "utf-8"
        stream.Open
        stream.WriteText content
        stream.SaveToFile fullPath, 2 'adSaveCreateOverWrite
        stream.Close
        Set stream = Nothing
        
        Set CreateFile = Me
    End Function
    
    '删除文件
    Public Function DeleteFile(byval path)
        Dim fullPath : fullPath = GetFullPath(path)
        If m_FSO.FileExists(fullPath) Then
            m_FSO.DeleteFile fullPath
        End If
        Set DeleteFile = Me
    End Function
    
    '复制文件
    Public Function CopyFile(byval source, byval target)
        Dim sourcePath : sourcePath = GetFullPath(source)
        Dim targetPath : targetPath = GetFullPath(target)
        If m_FSO.FileExists(sourcePath) Then
            CreateParentFolder m_FSO.GetParentFolderName(targetPath)
            m_FSO.CopyFile sourcePath, targetPath
        End If
        Set CopyFile = Me
    End Function
    
    '移动文件
    Public Function MoveFile(byval source, byval target)
        Dim sourcePath : sourcePath = GetFullPath(source)
        Dim targetPath : targetPath = GetFullPath(target)
        If m_FSO.FileExists(sourcePath) Then
            CreateParentFolder m_FSO.GetParentFolderName(targetPath)
            m_FSO.MoveFile sourcePath, targetPath
        End If
        Set MoveFile = Me
    End Function
    
    '读取文件
    Public Function ReadFile(byval path)
        Dim fullPath : fullPath = GetFullPath(path)
        If Not m_FSO.FileExists(fullPath) Then
            ReadFile = ""
            Exit Function
        End If
        
        Dim stream : Set stream = CreateObject("ADODB.Stream")
        stream.Type = 2 'adTypeText
        stream.Charset = "utf-8"
        stream.Open
        stream.LoadFromFile fullPath
        ReadFile = stream.ReadText
        stream.Close
        Set stream = Nothing
    End Function
    
    '读取二进制文件
    Public Function ReadBinary(byval path)
        Dim fullPath : fullPath = GetFullPath(path)
        If Not m_FSO.FileExists(fullPath) Then
            ReadBinary = Empty
            Exit Function
        End If
        
        Dim stream : Set stream = CreateObject("ADODB.Stream")
        stream.Type = 1 'adTypeBinary
        stream.Open
        stream.LoadFromFile fullPath
        ReadBinary = stream.Read
        stream.Close
        Set stream = Nothing
    End Function
    
    '获取文件信息
    Public Function GetFileInfo(byval path)
        Dim fullPath : fullPath = GetFullPath(path)
        If Not m_FSO.FileExists(fullPath) Then
            Set GetFileInfo = Nothing
            Exit Function
        End If
        
        Dim file : Set file = m_FSO.GetFile(fullPath)
        Dim info : Set info = Server.CreateObject("Scripting.Dictionary")
        info.Add "name", file.Name
        info.Add "path", Replace(file.Path, m_Root, "")
        info.Add "size", file.Size
        info.Add "type", file.Type
        info.Add "dateCreated", file.DateCreated
        info.Add "dateLastModified", file.DateLastModified
        info.Add "dateLastAccessed", file.DateLastAccessed
        Set GetFileInfo = info
    End Function
    
    '获取文件夹信息
    Public Function GetFolderInfo(byval path)
        Dim fullPath : fullPath = GetFullPath(path)
        If Not m_FSO.FolderExists(fullPath) Then
            Set GetFolderInfo = Nothing
            Exit Function
        End If
        
        Dim folder : Set folder = m_FSO.GetFolder(fullPath)
        Dim info : Set info = Server.CreateObject("Scripting.Dictionary")
        info.Add "name", folder.Name
        info.Add "path", Replace(folder.Path, m_Root, "")
        info.Add "size", folder.Size
        info.Add "files", folder.Files.Count
        info.Add "subFolders", folder.SubFolders.Count
        info.Add "dateCreated", folder.DateCreated
        info.Add "dateLastModified", folder.DateLastModified
        info.Add "dateLastAccessed", folder.DateLastAccessed
        Set GetFolderInfo = info
    End Function
    
    '获取文件列表
    Public Function GetFileList(byval path)
        Dim fullPath : fullPath = GetFullPath(path)
        If Not m_FSO.FolderExists(fullPath) Then
            GetFileList = Array()
            Exit Function
        End If
        
        Dim folder : Set folder = m_FSO.GetFolder(fullPath)
        Dim files : Set files = folder.Files
        Dim list(), i : i = 0
        ReDim list(files.Count - 1)
        
        Dim file
        For Each file In files
            Set list(i) = GetFileInfo(Replace(file.Path, m_Root, ""))
            i = i + 1
        Next
        
        GetFileList = list
    End Function
    
    '获取文件夹列表
    Public Function GetFolderList(byval path)
        Dim fullPath : fullPath = GetFullPath(path)
        If Not m_FSO.FolderExists(fullPath) Then
            GetFolderList = Array()
            Exit Function
        End If
        
        Dim folder : Set folder = m_FSO.GetFolder(fullPath)
        Dim folders : Set folders = folder.SubFolders
        Dim list(), i : i = 0
        ReDim list(folders.Count - 1)
        
        Dim subFolder
        For Each subFolder In folders
            Set list(i) = GetFolderInfo(Replace(subFolder.Path, m_Root, ""))
            i = i + 1
        Next
        
        GetFolderList = list
    End Function
    
    '获取完整路径
    Private Function GetFullPath(byval path)
        If Left(path, 1) = "/" Then path = Mid(path, 2)
        GetFullPath = m_Root & path
    End Function
    
    '创建父文件夹
    Private Sub CreateParentFolder(byval path)
        Dim parentPath : parentPath = m_FSO.GetParentFolderName(path)
        If Not m_FSO.FolderExists(parentPath) Then
            CreateParentFolder parentPath
            m_FSO.CreateFolder parentPath
        End If
    End Sub
    
    Private Sub Class_Terminate()
        Set m_FSO = Nothing
    End Sub
End Class
%> 